#include "PF_Config.h"

static volatile unsigned int ADCValue=0;
unsigned char bat_s=0x00;
//unsigned int g_wADCValue=0;
//static unsigned int s_wADCBuffer[ADC_BUFF_SIZE];
volatile unsigned char time_1s=0x00;

void Insert_TIM_ISR_Code(void)
{
	static unsigned char time=0x00;
	time++;
	if((time%2)==0) time_5ms|=0b00000010; //time_5ms 76543210 1-10ms ADC
	if((time%4)==1) time_5ms|=0b00000100; // 25,45,65... CS
	if(time==200) 
	{
		time_5ms|=0b00001000; //1s
		time_1s=0x01;
		time=0x00;
	}
}

void Insert_ADC_ISR_Code(void)
{
	sleep_off;
	ADCValue=ADC&0x03FF;
}

void g_adc(void)
{
	static unsigned char s_cBufferCounter=0;
	static unsigned long s_dTotal=0;
	s_dTotal-=value.s_wADCBuffer[s_cBufferCounter];
	CLI();
	value.s_wADCBuffer[s_cBufferCounter]=ADCValue;
	s_dTotal+=ADCValue;
	SEI();
	value.g_wADCValue=(unsigned long)s_dTotal>>ADC_BUFF_SIZE_BIT_COUNT;
	s_cBufferCounter++;
	if(s_cBufferCounter==ADC_BUFF_SIZE)
	{
		s_cBufferCounter=0;
	}
}

void g_bat(void)
{
	switch(bat_s)
	{
		case 0x00:
			if(ADCValue<710) bat_s=0xf0; //off
			else if(ADCValue<730) bat_s=0x00; //0
			else if(ADCValue<740) bat_s=0x01; //1
			else if(ADCValue<750) bat_s=0x02; //2
			else if(ADCValue>750) bat_s=0x03; //3
		break;
		case 0x01:
			if(ADCValue<710) bat_s=0xf0;
			else if(ADCValue<720) bat_s=0x00;
			else if(ADCValue<740) bat_s=0x01;
			else if(ADCValue<750) bat_s=0x02;
			else if(ADCValue>750) bat_s=0x03;
		break;
		case 0x02:
			if(ADCValue<710) bat_s=0xf0;
			else if(ADCValue<720) bat_s=0x00;
			else if(ADCValue<730) bat_s=0x01;
			else if(ADCValue<750) bat_s=0x02;
			else if(ADCValue>750) bat_s=0x03;
		break;
		case 0x03:
			if(ADCValue<710) bat_s=0xf0;
			else if(ADCValue<720) bat_s=0x00;
			else if(ADCValue<730) bat_s=0x01;
			else if(ADCValue<740) bat_s=0x02;
			else bat_s=0x03;
		break;
		default: bat_s=0x00;
	}
}